import React, { Component }  from 'react'
import { Table, Input, Icon, Button, Popconfirm, message, Modal } from 'antd';
import { fetchData  } from "../../../components/AjaxConsumer";
import style from './DateText.scss'


export default class EditableCell extends Component{
  constructor(props) {
      super(props);
      this.state = {
        value: this.props.value,
        dicData:this.props.dicData,
        editable: false,
      }
  }

  handleChange = (e) => {
      const value = e.target.value;
      this.setState({ value });
    }

    check = () => {//修改提交

      const {value, dicData } = this.state;
      if(value === ""){
        Modal.error({
            title: '错误提示',
            content: '不能为空',
        });
      }else if(!/[@#\$%\^&\*]+/g.test(value))//不为空和没有非法字符串执行提交
      {
        if (this.props.onChange) {
          this.props.onChange(this.state.value);
        }
        let options ={};
        let dictionaryId = dicData.dictionaryId;
        let su = '';
        console.log("===="+dicData.activeFlag+"====="+JSON.stringify(dicData));
        if(dicData.activeFlag){
           options= {
              url: '/base/dic/u',        //请求地址
              type: 'post',    //请求方式
              condition: {dictionaryId:dictionaryId,dicName:value},
          };
          su = '修改成功'
          console.log("修改成功=================================111");
        }else{
          options= {
             url: '/base/dic/c',        //请求地址
             type: 'post',    //请求方式
             condition: {dicName:value, dicCode:dicData.dicCode, dicType:dicData.dicType, dicTypeDesc:dicData.dicTypeDesc, activeFlag: 1},
           };
           su = '新增成功'
          console.log("新增------"+JSON.stringify(dicData));
        }
        fetchData(this, options, (resData) => {
          console.log("判断重复:"+JSON.stringify(resData));
          if(resData.code==1){
            this.setState({ editable: false });
            message.success(su);
          }
        });
      }else{
        Modal.error({
            title: '错误提示',
            content: '非法字符',
        });
      }
    }
    edit = () => {
      this.setState({ editable: true });
    }

  render() {
    const { value, editable } = this.state;

    return (
      <div className={style.editableCell}>
        {
          editable ?
            <div className={style.editableCellInputWrapper}>

              <Input
                className={style.InputWrapper}
                value={value}
                onChange={this.handleChange}
                onPressEnter={this.check}
              />
              <Icon
                type="check"
                className={style.editableCellCconCheck}
                onClick={this.check}
              />
            </div>
            :
            <div className={style.editableCellTextTrapper}>
              {value || ' '}
              <Icon
                type="edit"
                className={style.editableCellIcon}
                onClick={this.edit}
              />
            </div>
        }
      </div>
    );
  }
}
